package com.wangnian.wangcai.scheduled;

import com.wangnian.wangcai.dao.ChiGu;
import com.wangnian.wangcai.dao.ChiGuDao;
import com.wangnian.wangcai.dao.NameInfo;
import com.wangnian.wangcai.dao.NameInfoDao;
import com.wangnian.wangcai.service.api.ApiChannel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InitService {
    @Value("${spring.profiles.active}")
    private String environment;
    private final ChiGuDao chiGuDao;
    private final NameInfoDao nameInfoDao;

    public InitService(NameInfoDao nameInfoDao, ChiGuDao chiGuDao) {
        this.nameInfoDao = nameInfoDao;
        this.chiGuDao = chiGuDao;
    }


    public void init() {
        //syncService.runNameTHS("10天平均成交额大于3亿小于20亿；大于10元小于100元；30天涨跌幅小于0；非科创板；非北交所");
        if (environment.equals("prod")) {
            List<ChiGu> position = ApiChannel.apiService.position(0);
            for (ChiGu chiGu : position) {
                NameInfo nameInfo = nameInfoDao.findByCode(chiGu.getCode());
                if (nameInfo == null) {
                    nameInfo = new NameInfo();
                    nameInfo.setCode(chiGu.getCode());
                    nameInfo.setName(chiGu.getName());
                    nameInfo.setMonitor(true);
                    nameInfoDao.save(nameInfo);
                } else {
                    nameInfo.setMonitor(true);
                    nameInfoDao.save(nameInfo);
                }
            }
            chiGuDao.saveAll(position);
        }
    }

}
